//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using System.Text;
using Tathata.Common.Utils;
using Tathata.Common.BLL.EntityCollections;
using System.ComponentModel;
using Tathata.Common;

namespace Tathata.Web.Modules
{
    public partial class HeaderControl : BaseNopUserControl
    {
        private void ApplyLocalization()
        {
            btnSearch.Text = GetLocaleResourceString("Common.SearchButton");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ApplyLocalization();

            if (!Page.IsPostBack)
            {
                BindCurrencies();
                BindLanguages();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtSearchTerms.Text))
            {
                Response.Redirect(string.Format("~/search.aspx?searchTerms={0}", HttpUtility.UrlEncode(txtSearchTerms.Text)));
            }
        }

        private void BindCurrencies()
        {
            CurrencyCollection currencies = CurrencyManager.GetAllCurrencies();
            if (currencies.Count > 1)
            {
                ddlCurrencies.Visible = true;
                this.ddlCurrencies.Items.Clear();
                Currency customerCurrency = NopContext.Current.WorkingCurrency;
                foreach (Currency currency in currencies)
                {
                    ListItem item = new ListItem(currency.Name, currency.CurrencyID.ToString());
                    this.ddlCurrencies.Items.Add(item);

                }
                if (customerCurrency != null)
                    CommonHelper.SelectListItem(this.ddlCurrencies, customerCurrency.CurrencyID);
            }
            else
                selectCurrencyBlock.Visible = false;
        }

        private void BindLanguages()
        {
            LanguageCollection languages = LanguageManager.GetAllLanguages();
            if (languages.Count > 1)
            {
                ddlLanguages.Visible = true;
                this.ddlLanguages.Items.Clear();
                Language customerLanguage = NopContext.Current.WorkingLanguage;
                foreach (Language language in languages)
                {
                    ListItem item = new ListItem(language.Name, language.LanguageID.ToString());
                    this.ddlLanguages.Items.Add(item);

                }
                if (customerLanguage != null)
                    CommonHelper.SelectListItem(this.ddlLanguages, customerLanguage.LanguageID);
            }
            else
                selectLanguageBlock.Visible = false;
        }

        protected void ddlCurrencies_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            int currencyID = int.Parse(this.ddlCurrencies.SelectedItem.Value);
            Currency currency = CurrencyManager.GetByCurrencyID(currencyID);
            if (currency != null && currency.Published)
            {
                NopContext.Current.WorkingCurrency = currency;
                CommonHelper.ReloadCurrentPage();
            }
        }

        protected void ddlLanguages_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            int languagesID = int.Parse(this.ddlLanguages.SelectedItem.Value);
            Language language = LanguageManager.GetByLanguageID(languagesID);
            if (language != null && language.Published)
            {
                NopContext.Current.WorkingLanguage = language;
                CommonHelper.ReloadCurrentPage();
            }
        }
    }
}